package com.example.denglu.servlet;

import com.alibaba.fastjson.JSONObject;
import com.example.denglu.service.UserService;
import com.example.denglu.util.SessionManager;
import com.example.denglu.util.WeChatUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/wechatLogin")
public class WeChatLoginServlet extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws IOException {
        String action = request.getParameter("action");
        response.setContentType("application/json;charset=utf-8");
        
        if ("getQrCode".equals(action)) {
            // 生成二维码
            String sceneStr = WeChatUtil.generateSceneStr();
            JSONObject qrCodeInfo = WeChatUtil.createTempQRCode(sceneStr);
            
            // 保存场景值到session
            HttpSession session = request.getSession();
            session.setAttribute("wechat_scene", sceneStr);
            
            // 返回二维码信息和场景值
            JSONObject result = new JSONObject();
            result.put("ticket", qrCodeInfo.getString("ticket"));
            result.put("sceneStr", sceneStr);
            response.getWriter().write(result.toJSONString());
            
        } else if ("checkLogin".equals(action)) {
            String sceneStr = request.getParameter("sceneStr");
            System.out.println("检查登录状态，sceneStr=" + sceneStr);
            
            Boolean scanned = (Boolean) SessionManager.getAttribute(sceneStr, "wechat_scanned");
            String openid = (String) SessionManager.getAttribute(sceneStr, "wechat_openid");
            Boolean needPhone = (Boolean) SessionManager.getAttribute(sceneStr, "need_phone");
            Boolean needSetPassword = (Boolean) SessionManager.getAttribute(sceneStr, "need_set_password");
            
            JSONObject result = new JSONObject();
            result.put("scanned", scanned != null && scanned);
            
            if (openid != null) {
                if (needPhone != null && needPhone) {
                    // 需要绑定手机号
                    result.put("success", false);
                    result.put("needPhone", true);
                    result.put("message", "请在微信中发送手机号进行绑定");
                } else if (needSetPassword != null && needSetPassword) {
                    // 需要设置密码
                    result.put("success", true);
                    result.put("redirectUrl", "set-password.html");
                    // 保存openid到session
                    HttpSession session = request.getSession();
                    session.setAttribute("wechat_openid", openid);
                } else {
                    // 可以直接登录
                    result.put("success", true);
                    result.put("redirectUrl", "game-home.html");
                    // 保存openid到session
                    HttpSession session = request.getSession();
                    session.setAttribute("wechat_openid", openid);
                }
                
                // 清理临时数据
                if (result.getBooleanValue("success")) {
                    SessionManager.removeSession(sceneStr);
                }
            } else {
                result.put("success", false);
            }
            
            System.out.println("返回结果：" + result.toJSONString());
            response.getWriter().write(result.toJSONString());
        }
    }
} 